import httpx

# response = httpx.get('https://www.httpbin.org/get')
# print(response.status_code)
# print(response.headers)
# print(response.text)

# 使用httpx请求HTTP/2.0的网站需要先声明使用http2协议
# client = httpx.Client(http2=True)
# res = client.get('https://spa16.scrape.center/')
# print(res.text)


# Client对象的使用
# 官方推荐用法
# with httpx.Client() as client:
#     res = client.get('https://www.httpbin.org/get')
#     print(res)
# # 上述用法等价于：
# client = httpx.Client()
# try:
#     res = client.get('https://www.httpbin.org/get')
# finally:
#     client.close() # 这个必须带

# Client对象携带参数
# url = 'http://www.httpbin.org/headers'
# headers = {'User-Agent': 'my-app/0.0.1'}
# with httpx.Client(headers=headers) as client:
#     r = client.get(url)
#     print(r.json()['headers']['User-Agent'])

# http协议版本
# client = httpx.Client(http2=True)
# res = client.get('https://httpbin.org/get')
# print(res.text)
# print(res.http_version)


# httpx异步请求
import asyncio
async def fetch(url):
    async with httpx.AsyncClient(http2=True) as client:
        res = await client.get(url)
        print(res.text)

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(fetch('https://www.httpbin.org/get'))
